package com.csap.framework.validation.factory;

import com.csap.framework.properties.apidoc.ModelType;
import com.csap.framework.util.map.CsapMap;
import com.csap.mybatisplus.core.toolkit.StringPool;
import com.csap.mybatisplus.extension.activerecord.CloneModel;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author yangchengfu
 * @description 数据验证基础模型
 * @dataTime 2021年-01月-22日 22:56:00
 **/
@Data
@NoArgsConstructor
public class BaseModel extends CloneModel<BaseModel, BaseModel> {
    /**
     * 当前参数泛型列表
     */
    private List<Type> types;
    /**
     * 当前key累加名称
     */
    private StringBuilder keyName = new StringBuilder();
    /**
     * 键名称
     */
    private String key;
    /**
     * 当前模型class
     */
    private Class<?> modelClass;
    /**
     * 当前验证字段对象
     */
    private CsapMap<String, Validate.ValidateField> validateField;

    private Class field;

    /**
     * 对象类型
     */
    private ModelType modelType;

    public static BaseModel build() {
        return new BaseModel();
    }

    public BaseModel field(Class field) {
        this.field = field;
        return this;
    }

    public BaseModel modelType(ModelType modelType) {
        this.modelType = modelType;
        return this;
    }

    public BaseModel types(List<Type> types) {
        this.types = types;
        return this;
    }

    public BaseModel addTypes(List<Type> types) {
        if (this.types == null) {
            this.types = Lists.newArrayList(types);
        } else {
            this.types.addAll(types);
        }
        return this;
    }

    public BaseModel validateField(CsapMap<String, Validate.ValidateField> validateField) {
        this.validateField = validateField;
        return this;
    }

    public BaseModel appendKeyName(String name) {
        keyName.append(name).append(StringPool.DOT);
        return this;
    }

    public BaseModel keyName(String name) {
        if (keyName.length() > 0) {
            keyName.setLength(0);
        }
        keyName.append(name).append(StringPool.DOT);
        return this;
    }

    public BaseModel key(String name) {
        key = name;
        return this;
    }

    public BaseModel modelClass(Class<?> modelClass) {
        this.modelClass = modelClass;
        return this;
    }

    public BaseModel addKey(String name) {
        key = key == null ? name : key + StringPool.DOT + name;
        return this;
    }

    public String toStringKeyName() {
        return keyName.substring(0, keyName.length() - 1);
    }
}
